﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Transactions;
namespace DVSProject.Lib
{
    public abstract class RepositoryBase
    {
        private SqlConnection _connection;
        //protected log4net.ILog _log;

        public RepositoryBase(SqlConnection connection)
        {
            if (connection == null) { throw new ArgumentNullException("connection"); }
            _connection = connection;
            // _log = log4net.LogManager.GetLogger(this.GetType());
        }

        public SqlConnection Connection
        {
            get { return _connection; }
        }

        protected SqlCommand CreateCommandEnsureConnectionOpen(SqlConnection _connection)
        {
            if (_connection.State == System.Data.ConnectionState.Closed) { _connection.Open(); }
            Connection.EnlistTransaction(Transaction.Current); // ถ้าไม่ใส่ ต้องเปิด transaction ก่อนสร้าง connection เสมอ ** 
            return _connection.CreateCommand();
        }
    }
}